home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 1.toast / Sample Code / Devices and Hardware / Drivers / Load PCI Driver / FullPath.c next >
Encoding:
C/C++ Source or Header  |  2000-09-28  |  7.4 KB  |  242 lines  |  [TEXT/CWIE]

  1. /*
  2.     File:        FullPath.c
  3.  
  4.     Contains:    Routines for dealing with full pathnames... if you really must.
  5.  
  6.     Written by:     
  7.  
  8.     Copyright:    Copyright © 1995-1999 by Apple Computer, Inc., All Rights Reserved.
  9.  
  10.                 You may incorporate this Apple sample source code into your program(s) without
  11.                 restriction. This Apple sample source code has been provided "AS IS" and the
  12.                 responsibility for its operation is yours. You are not permitted to redistribute
  13.                 this Apple sample source code as "Apple sample source code" after having made
  14.                 changes. If you're going to re-distribute the source, we require that you make
  15.                 it clear in the source that the code was descended from Apple sample source
  16.                 code, but that you've made changes.
  17.  
  18.     Change History (most recent first):
  19.                 8/3/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  20.                 
  21.  
  22. */
  23.  
  24. #include <Types.h>
  25. #include <Errors.h>
  26. #include <Memory.h>
  27. #include <Files.h>
  28. #include <TextUtils.h>
  29. #include <Aliases.h>
  30.  
  31. #include "FSpCompat.h"
  32. #include "FullPath.h"
  33.  
  34. #define    __COMPILINGMOREFILES
  35.  
  36. /*
  37.     IMPORTANT NOTE:
  38.     
  39.     The use of full pathnames is strongly discouraged. Full pathnames are
  40.     particularly unreliable as a means of identifying files, directories
  41.     or volumes within your application, for two primary reasons:
  42.     
  43.     •     The user can change the name of any element in the path at virtually
  44.         any time.
  45.     •    Volume names on the Macintosh are *not* unique. Multiple
  46.         mounted volumes can have the same name. For this reason, the use of
  47.         a full pathname to identify a specific volume may not produce the
  48.         results you expect. If more than one volume has the same name and
  49.         a full pathname is used, the File Manager currently uses the first
  50.         mounted volume it finds with a matching name in the volume queue.
  51.     
  52.     In general, you should use a file’s name, parent directory ID, and
  53.     volume reference number to identify a file you want to open, delete,
  54.     or otherwise manipulate.
  55.     
  56.     If you need to remember the location of a particular file across
  57.     subsequent system boots, use the Alias Manager to create an alias record
  58.     describing the file. If the Alias Manager is not available, you can save
  59.     the file’s name, its parent directory ID, and the name of the volume on
  60.     which it’s located. Although none of these methods is foolproof, they are
  61.     much more reliable than using full pathnames to identify files.
  62.     
  63.     Nonetheless, it is sometimes useful to display a file’s full pathname to
  64.     the user. For example, a backup utility might display a list of full
  65.     pathnames of files as it copies them onto the backup medium. Or, a
  66.     utility might want to display a dialog box showing the full pathname of
  67.     a file when it needs the user’s confirmation to delete the file. No
  68.     matter how unreliable full pathnames may be from a file-specification
  69.     viewpoint, users understand them more readily than volume reference
  70.     numbers or directory IDs. (Hint: Use the TruncString function from
  71.     TextUtils.h with truncMiddle as the truncWhere argument to shorten
  72.     full pathnames to a displayable length.)
  73.     
  74.     The following technique for constructing the full pathname of a file is
  75.     intended for display purposes only. Applications that depend on any
  76.     particular structure of a full pathname are likely to fail on alternate
  77.     foreign file systems or under future system software versions.
  78. */
  79.  
  80. /*****************************************************************************/
  81.  
  82. pascal    OSErr    GetFullPath(short vRefNum,
  83.                             long dirID,
  84.                             ConstStr255Param name,
  85.                             short *fullPathLength,
  86.                             Handle *fullPath)
  87. {
  88.     OSErr        result;
  89.     FSSpec        spec;
  90.     
  91.     *fullPathLength = 0;
  92.     *fullPath = NULL;
  93.     
  94.     result = FSMakeFSSpecCompat(vRefNum, dirID, name, &spec);
  95.     if ( result == noErr )
  96.     {
  97.         result = FSpGetFullPath(&spec, fullPathLength, fullPath);
  98.     }
  99.     
  100.     return ( result );
  101. }
  102.  
  103. /*****************************************************************************/
  104.  
  105. pascal    OSErr    FSpGetFullPath(const FSSpec *spec,
  106.                                short *fullPathLength,
  107.                                Handle *fullPath)
  108. {
  109.     OSErr        result;
  110.     FSSpec        tempSpec;
  111.     CInfoPBRec    pb;
  112.     
  113.     *fullPathLength = 0;
  114.     *fullPath = NULL;
  115.     
  116.     /* Make a copy of the input FSSpec that can be modified */
  117.     BlockMoveData(spec, &tempSpec, sizeof(FSSpec));
  118.     
  119.     if ( tempSpec.parID == fsRtParID )
  120.     {
  121.         /* The object is a volume */
  122.         
  123.         /* Add a colon to make it a full pathname */
  124.         ++tempSpec.name[0];
  125.         tempSpec.name[tempSpec.name[0]] = ':';
  126.         
  127.         /* We're done */
  128.         result = PtrToHand(&tempSpec.name[1], fullPath, tempSpec.name[0]);
  129.     }
  130.     else
  131.     {
  132.         /* The object isn't a volume */
  133.         
  134.         /* Is the object a file or a directory? */
  135.         pb.dirInfo.ioNamePtr = tempSpec.name;
  136.         pb.dirInfo.ioVRefNum = tempSpec.vRefNum;
  137.         pb.dirInfo.ioDrDirID = tempSpec.parID;
  138.         pb.dirInfo.ioFDirIndex = 0;
  139.         result = PBGetCatInfoSync(&pb);
  140.         if ( result == noErr )
  141.         {
  142.             /* if the object is a directory, append a colon so full pathname ends with colon */
  143.             if ( (pb.hFileInfo.ioFlAttrib & ioDirMask) != 0 )
  144.             {
  145.                 ++tempSpec.name[0];
  146.                 tempSpec.name[tempSpec.name[0]] = ':';
  147.             }
  148.             
  149.             /* Put the object name in first */
  150.             result = PtrToHand(&tempSpec.name[1], fullPath, tempSpec.name[0]);
  151.             if ( result == noErr )
  152.             {
  153.                 /* Get the ancestor directory names */
  154.                 pb.dirInfo.ioNamePtr = tempSpec.name;
  155.                 pb.dirInfo.ioVRefNum = tempSpec.vRefNum;
  156.                 pb.dirInfo.ioDrParID = tempSpec.parID;
  157.                 do    /* loop until we have an error or find the root directory */
  158.                 {
  159.                     pb.dirInfo.ioFDirIndex = -1;
  160.                     pb.dirInfo.ioDrDirID = pb.dirInfo.ioDrParID;
  161.                     result = PBGetCatInfoSync(&pb);
  162.                     if ( result == noErr )
  163.                     {
  164.                         /* Append colon to directory name */
  165.                         ++tempSpec.name[0];
  166.                         tempSpec.name[tempSpec.name[0]] = ':';
  167.                         
  168.                         /* Add directory name to beginning of fullPath */
  169.                         (void) Munger(*fullPath, 0, NULL, 0, &tempSpec.name[1], tempSpec.name[0]);
  170.                         result = MemError();
  171.                     }
  172.                 } while ( (result == noErr) && (pb.dirInfo.ioDrDirID != fsRtDirID) );
  173.             }
  174.         }
  175.     }
  176.     if ( result == noErr )
  177.     {
  178.         /* Return the length */
  179.         *fullPathLength = GetHandleSize(*fullPath);
  180.     }
  181.     else
  182.     {
  183.         /* Dispose of the handle and return NULL and zero length */
  184.         if ( *fullPath != NULL )
  185.         {
  186.             DisposeHandle(*fullPath);
  187.         }
  188.         *fullPath = NULL;
  189.         *fullPathLength = 0;
  190.     }
  191.     
  192.     return ( result );
  193. }
  194.  
  195. /*****************************************************************************/
  196.  
  197. pascal OSErr FSpLocationFromFullPath(short fullPathLength,
  198.                                      const void *fullPath,
  199.                                      FSSpec *spec)
  200. {
  201.     AliasHandle    alias;
  202.     OSErr        result;
  203.     Boolean        wasChanged;
  204.     Str32        nullString;
  205.     
  206.     /* Create a minimal alias from the full pathname */
  207.     nullString[0] = 0;    /* null string to indicate no zone or server name */
  208.     result = NewAliasMinimalFromFullPath(fullPathLength, fullPath, nullString, nullString, &alias);
  209.     if ( result == noErr )
  210.     {
  211.         /* Let the Alias Manager resolve the alias. */
  212.         result = ResolveAlias(NULL, alias, spec, &wasChanged);
  213.         
  214.         DisposeHandle((Handle)alias);    /* Free up memory used */
  215.     }
  216.     return ( result );
  217. }
  218.  
  219. /*****************************************************************************/
  220.  
  221. pascal OSErr LocationFromFullPath(short fullPathLength,
  222.                                   const void *fullPath,
  223.                                   short *vRefNum,
  224.                                   long *parID,
  225.                                   Str31 name)
  226. {
  227.     OSErr    result;
  228.     FSSpec    spec;
  229.     
  230.     result = FSpLocationFromFullPath(fullPathLength, fullPath, &spec);
  231.     if ( result == noErr )
  232.     {
  233.         *vRefNum = spec.vRefNum;
  234.         *parID = spec.parID;
  235.         BlockMoveData(&spec.name[0], &name[0], spec.name[0] + 1);
  236.     }
  237.     return ( result );
  238. }
  239.  
  240. /*****************************************************************************/
  241.  
  242.